package cn.sxt.clinic.service.impl;

import cn.sxt.clinic.pojo.Job;
import cn.sxt.clinic.pojo.Permission;
import cn.sxt.clinic.mapper.PermissionMapper;
import cn.sxt.clinic.service.IJobService;
import cn.sxt.clinic.service.IPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Autowired
    private IJobService jobService;

    @Override
    public List<String> getJobExpressions(String jobName) {

        QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();
        jobQueryWrapper.eq("job_name", jobName);
        Job job = jobService.getOne(jobQueryWrapper);
        QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.in("permission_id", job.getPermissionIds().split(","));
        List<Permission> permissions = baseMapper.selectList(permissionQueryWrapper);
        List<String> expressions = new ArrayList<>();

        for (Permission permission : permissions) {
            expressions.add(permission.getExpression());
        }
        return expressions;
    }
}
